import 'dart:math';

import 'package:flutter/material.dart';

class BuildList extends StatefulWidget {
  BuildList({super.key, required this.point});
  String point;
  @override
  State<BuildList> createState() => _BuildListState();
}

class _BuildListState extends State<BuildList> {
  Map data = {
    "point": "", // 记录小区信息
    "size": 0, // 随机产生楼栋的数量
    "type": "", // 小区的楼栋名称(size>4 单元, size<=4 号楼)
  };

  @override
  void initState() {
    super.initState();
    getRandom();
  }

//产生随机楼栋
// 需求:生成3-8个随机数,如果随机数>5,展示单元,否则展示栋
  getRandom() {
    Random rand = Random();
    int size = rand.nextInt(6) + 3;
    String type = size < 5 ? "单元" : "栋";
    setState(() {
      data['point'] = widget.point;
      data['size'] = size;
      data['type'] = type;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('选择楼栋'),
        backgroundColor: Colors.transparent,
      ),
      body: Container(
        child: ListView.separated(
          itemCount: data['size'],
          separatorBuilder: (BuildContext context, int index) {
            return const Divider(
              height: 2,
              color: Colors.grey,
            );
          },
          itemBuilder: (BuildContext context, int index) {
            return GestureDetector(
              onTap: () {
                Navigator.pushNamed(context, '/room_list', arguments: {
                  'point': data['point'],
                  'building': '${index + 1}${data['type']}'
                });
              },
              child: Container(
                padding: const EdgeInsets.all(10),
                height: 50,
                color: Colors.white,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text('${data['point']}${index + 1}${data['type']}'),
                    const Icon(
                      Icons.arrow_forward_ios,
                      size: 20,
                    )
                  ],
                ),
              ),
            );
          },
        ),
      ),
    );
  }
}
